<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['kechengid'])){
		$params['kechengid']=intval($_RQ['kechengid']);
		$return['params']['kechengid']=intval($_RQ['kechengid']);
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_peiedu_xuesheng',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['pcd']=json_decode($item['pcd'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'学生管理'),
			array('op'=>'edit','title'=>'添加学生'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(		
			array('name'=>'title','title'=>'姓名','type'=>'string'),
			array('name'=>'kechengid','title'=>'课程','type'=>'select','options'=>'kechengs'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'codenumber','title'=>'身份证','type'=>'string'),
			array('name'=>'pcd','title'=>'学生所在地','type'=>'pcd'),
			array('name'=>'email','title'=>'邮箱地址','type'=>'string'),
			array('name'=>'username','title'=>'用户名称','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'zhaopian','title'=>'2寸蓝底照片','type'=>'image'),
			
			array('name'=>'cert','title'=>'证书','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
		),'action'=>array(
			array('type'=>'modal','op'=>'setstatus','title'=>'审核操作'),
			array('type'=>'href','action'=>'/xuesheng/fa/','title'=>'发放证书','show'=>array('status'=>1)),
			array('type'=>'href','action'=>'/xuesheng/cert/','title'=>'查看证书','show'=>array('status'=>2)),
			array('type'=>'href','action'=>'/xuesheng/edit/','title'=>'查看并编辑'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'type'=>'string','title'=>'学生姓名'),
			array('name'=>'kechengid','width'=>15,'title'=>'课程','type'=>'select','options'=>'kechengs'),
		)
	);
	$return['kechengs']=pdo_getall('jy_peiedu_kecheng',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['status']=$this->xuesheng_status;	
	$return['sex']=$this->sex;	
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_xuesheng',array('id'=>$id));
	}else{
		$item=array('status'=>1);
	}
	$item['pcd']=json_decode($item['pcd'],true);
	$item['password']='';
    $return['item']=$item;
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'学生资料',
		'tabs'=>array(
			array('op'=>'list','title'=>'学生管理'),
			array('op'=>'edit','title'=>'添加学生')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'姓名','type'=>'string','must'=>true),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'codenumber','title'=>'身份证','type'=>'string','must'=>true),
			array('name'=>'pcd','title'=>'学生所在地','type'=>'pcd'),
			array('name'=>'email','title'=>'邮箱地址','type'=>'string'),
			array('name'=>'username','title'=>'用户名称','type'=>'string'),
			array('name'=>'password','title'=>'密码','type'=>'password'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'zhaopian','title'=>'2寸蓝底照片','type'=>'image','must'=>true),
			array('name'=>'kechengid','title'=>'选择课程','type'=>'select','options'=>'kechengs'),
			//array('name'=>'codeimage01','title'=>'身份证正面','type'=>'image','must'=>true),
			//array('name'=>'codeimage02','title'=>'身份证反面','type'=>'image','must'=>true),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','show'=>array('status'=>array(0,1))),			
		)
	);
	$return['kechengs']=pdo_getall('jy_peiedu_kecheng',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['status']=array('待审核','正常');
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title'] || !$postdata['sex']){
		exi('请填写姓名和性别！','error');
	}
	if(!$postdata['zhaopian'] ){
		exi('请上传2寸蓝底照片','error');
	}
	if(!$postdata['codenumber'] ){
		exi('请填写身份证号','error');
	}
	if(!$postdata['codeimage01'] ){
		//exi('请上传身份证正面','error');
	}
	if(!$postdata['codeimage02'] ){
		//exi('请上传身份证反面','error');
	}
	$postdata['pcd']=ijson_encode($postdata['pcd']);
	$data=array_fields(array(
		'type',
		'title',
		'number',
		'kechengid',
		'sex',
		'mobile',
		'email',
		'username',
		'pcd',
		'zhaopian',
		'codenumber',
		'codeimage01',
		'codeimage02',
		'paixu',
		'remark',
		'status',
	),$postdata);
	$id=intval($postdata['id']);
	if($postdata['password']){
		$data['password']=base64_encode($postdata['password']);
	}
	if($id){
		pdo_update('jy_peiedu_xuesheng',$data,array('id'=>$id));
		exi('设置成功！','','/xuesheng/');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_peiedu_xuesheng',$data);
		exi('添加成功！','','/xuesheng/');
	}
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$list=pdo_getall('jy_peiedu_xuesheng',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($list as &$v){
		$v['pici_id']=pdo_getcolumn('jy_chengedu_pici',array('id'=>$v['pici_id'],'uniacid'=>$_SESSION['uniacid']),'title');
		$v['type']=pdo_getcolumn('jy_peiedu_xuesheng_type',array('id'=>$v['type'],'uniacid'=>$_SESSION['uniacid']),'title');
		$v['zhuanye_id']=pdo_getcolumn('jy_chengedu_zhuanye',array('id'=>$v['zhuanye_id'],'uniacid'=>$_SESSION['uniacid']),'title');
		$v['ceng_type']=pdo_getcolumn('jy_chengedu_ceng_type',array('id'=>$v['ceng_type'],'uniacid'=>$_SESSION['uniacid']),'title');
		$v['school_id']=pdo_getcolumn('jy_chengedu_school',array('id'=>$v['school_id'],'uniacid'=>$_SESSION['uniacid']),'title');
		$v['status']=$this->xuesheng_status[$v['status']];
	}
	$header=array(
		'pici_id'=>'批次',
		'type'=>'类型',
		'zhuanye_id'=>'专业',
		'ceng_type'=>'层次',
		'school_id'=>'学校',
		'name'=>'姓名',
		'codenumber'=>'身份证',
		'status'=>'状态',
	);
	cfc('tools')->export2excel($header,$list,'学生');
}
if($_RQ['op']=='setstatus'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$update=array('status'=>intval($post['status']));
		pdo_update('jy_peiedu_xuesheng',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_peiedu_xuesheng',array('id'=>$id),array('id','status'));
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'审核操作',
	);
	$htmls['edit']=array(
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
	);
	$return['htmls']=$htmls;
	$return['status']=$this->xuesheng_status;
	exi($return);
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['sn']){
			exi('请输入证书编号！','error');
		}
		$update=array('status'=>2,'cert'=>$post['cert']);
		pdo_update('jy_peiedu_xuesheng',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$cret=pdo_get('jy_peiedu_cert',array('uniacid'=>$_SESSION['uniacid'],'kechengid'=>$post['kechengid'],'xueshengid'=>$id));
		$data=array(
			'kechengid'=>$post['kechengid'],
			'codenumber'=>$post['codenumber'],
			'sn'=>$post['sn'],
			'image'=>$post['cert'],
		);
		if($cret['id']){
			pdo_update('jy_peiedu_cert',$data,array('uniacid'=>$_SESSION['uniacid'],'kechengid'=>$post['kechengid'],'xueshengid'=>$id));
			exi('更新成功！','','//list/');
		}else{
			$data['xueshengid']=$post['id'];
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			pdo_insert('jy_peiedu_cert',$data);
			exi('发证成功！','','//list/');
		}
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();
	}
	$kecheng=pdo_getcolumn('jy_peiedu_kecheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['kechengid']),'title');
	$poster=$this->plugin_setting('poster');
	$diydata=$poster['data']['data'];
	$url=custom_url('jy_peiedu/cert//xueshengid='.$item['id'],true);
	$qr=cfc('tools')->createqrcode($url,150,true);
	foreach($diydata as &$dv){
		if($dv['type']=='zhaopian'){
			$dv['type']='img';
			$dv['src']=$item['zhaopian'];
		}elseif($dv['type']=='qr'){
			$dv['type']='img';
			$dv['src']=$qr;
		}elseif($dv['type']=='title'){
			$dv['type']='text';
			$dv['value']=$item['title'];
		}elseif($dv['type']=='title'){
			$dv['type']='text';
			$dv['value']=$item['title'];
		}elseif($dv['type']=='sex'){
			$dv['type']='text';
			$dv['value']=$this->sex[$item['sex']];
		}elseif($dv['type']=='codenumber'){
			$dv['type']='text';
			$dv['value']=$item['codenumber'];
		}elseif($dv['type']=='kecheng'){
			$dv['type']='text';
			$dv['value']=$kecheng;
		}elseif($dv['type']=='sn'){
			$dv['type']='text';
			$dv['value']=$item['sn'];
		}
	}
	$poster=array(
		'id'=>'jy_peiedu',
		'height'=>990,
		'width'=>1360,
		'bg'=>$poster['data']['image']?:'../addons/jy_peiedu/images/cert.jpg',
		'data'=>$diydata,
		'diywidth'=>960
	);
	$item['cert']=model('jy_poster/model')->createPoster($poster,'','',$id,'jy_peiedu');
	$item['sex']=$this->sex[$item['sex']];
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'发放证书',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回学生列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'姓名','type'=>'copy'),
			array('name'=>'mobile','title'=>'手机号','type'=>'copy'),
			array('name'=>'codenumber','title'=>'身份证','type'=>'copy'),
			array('name'=>'sex','title'=>'身份证','type'=>'copy'),
			array('name'=>'zhaopian','title'=>'2寸蓝底照片','type'=>'img_show'),
			array('name'=>'cert','title'=>'证书效果图','type'=>'img_show'),		
			array('name'=>'sn','title'=>'证书编号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='cert'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peiedu_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();
	}
	$item['sex']=$this->sex[$item['sex']];
    $return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'证书',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回学生列表'),
		),
		'look'=>array(
			array('name'=>'title','title'=>'姓名','type'=>'copy'),
			array('name'=>'mobile','title'=>'手机号','type'=>'copy'),
			//array('name'=>'codenumber','title'=>'身份证','type'=>'copy'),
			//array('name'=>'sex','title'=>'身份证','type'=>'copy'),
			//array('name'=>'zhaopian','title'=>'2寸蓝底照片','type'=>'image'),
			array('name'=>'cert','title'=>'证书','type'=>'image'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_peiedu_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_peiedu_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>